package main

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/transform"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"regexp"
)

func main() {
	url := "https://www.zhenai.com/zhenghun"
	client := &http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.51")
	cookie1 := "sid=22c06446-5b13-435b-9851-6c979acb8c1b; ec=GRPjn2rd-1619706850959-a98821a967df8-397052183; __channelId=905821%2C0; _exid=WWTXBv6UfFGLh6DixzBFlqJ1tndsD0kFRolkNunAkGVIGvBFUDsIkE6nGwJfMWAgVfVv25o2P8P%2BgAu6%2F7K0Aw%3D%3D; _efmdata=HoRW%2F28pBX%2FErnvuYnqM8KlguR5aGg8%2F446%2BPhctK2J5cbSrgcTgnSffWELw%2BVNW%2B0dQI1Kb0hSWmXmA%2FLMwvWzUFxUTWMFjaJOruq7IRsU%3D; Hm_lvt_2c8ad67df9e787ad29dbd54ee608f5d2=1619706866,1619769263; Hm_lpvt_2c8ad67df9e787ad29dbd54ee608f5d2=1619770987"
	req.Header.Add("cookie", cookie1)

	resp, err := client.Do(req)
	if err != nil {
		log.Fatalln(err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		fmt.Println("Error: status code", resp.StatusCode)
		return
	}

	e := determineEncoding(resp.Body)
	utf8Reader := transform.NewReader(resp.Body, e.NewDecoder())

	all, err := ioutil.ReadAll(utf8Reader)
	if err != nil {
		panic(err)
	}
	//fmt.Printf("%s\n", all)

	printCityList(all)
}

func determineEncoding(r io.Reader) encoding.Encoding {
	bytes, err := bufio.NewReader(r).Peek(1024)
	if err != nil {
		panic(err)
	}
	e, _, _ := charset.DetermineEncoding(bytes, "")
	return e
}

func printCityList(contents []byte) {
	re := regexp.MustCompile(`<a href="(http://www.zhenai.com/zhenghun/[a-z0-9]+)"[^>]*>([^<]+)</a>`)
	matches := re.FindAllSubmatch(contents, -1)
	for _, m := range matches {
		fmt.Printf("City: %s, URL: %s\n", m[2], m[1])
	}
	fmt.Println(len(matches))
}

//<a href="http://www.zhenai.com/zhenghun/linfen" data-v-1573aa7c>临汾</a>

